﻿Imports System.Text
Imports Microsoft.SharePoint.Taxonomy

Public Class SolidQTermSetExporter
    Public Function ExportTermSet(ByVal termSet As TermSet) As String
        Dim exportData As New StringBuilder()
        exportData.AppendLine("""Term Set Name"",""Term Set Description"",""LCID"",""Available for Tagging"",""Term Description"",""Level 1 Term"",""Level 2 Term"",""Level 3 Term"",""Level 4 Term"",""Level 5 Term"",""Level 6 Term"",""Level 7 Term""")

        For Each topTerm In termSet.Terms
            exportData.AppendLine(String.Format("""{0}"",""{1}"",,{2},""{3}"",""{4}"",,,,,,,", termSet.Name, termSet.Description, termSet.IsAvailableForTagging, topTerm.GetDescription(), topTerm.Name))

            For Each childTerm In topTerm.Terms
                ExportTerm(exportData, childTerm, String.Format("""{0}""", topTerm.Name))
            Next
        Next

        Return exportData.ToString()
    End Function

    Private Sub ExportTerm(ByVal exportData As StringBuilder, ByVal term As Term, ByVal heritage As String)
        heritage = String.Concat(heritage, ",", String.Format("""{0}""", term.Name))

        exportData.AppendLine(String.Format(",,,{0},""{1}"",{2}", term.IsAvailableForTagging, term.GetDescription(), heritage))

        For Each subTerm In term.Terms
            ExportTerm(exportData, subTerm, heritage)
        Next
    End Sub
End Class
